import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
// TODO 引入自定义dialog
import "./wiget/myDialog.dart";

void main() {
  runApp(const MaterialApp(home: HomePage()));
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // TODO showDialog
  void _alertDialog() async {
    var res = await showDialog(
      barrierDismissible: false, // 点击背景不关闭
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('提示'),
          content: Text('这是一个对话框。'),
          actions: [
            TextButton(
              child: const Text("取消"),
              onPressed: () {
                Navigator.of(context).pop('cancel');
              },
            ),
            TextButton(
              child: const Text("确定"),
              onPressed: () {
                Navigator.of(context).pop('ok');
              },
            )
          ],
        );
      },
    );
    print(res);
  }

  // TODO SimpleDialog
  void _simpleDialog() async {
    var res = await showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: Text('请选择语言'),
          children: [
            SimpleDialogOption(
              child: Text('中文'),
              onPressed: () {
                Navigator.of(context).pop('中文');
              },
            ),
            SimpleDialogOption(
              child: Text('英文'),
              onPressed: () {
                Navigator.of(context).pop('英文');
              },
            )
          ],
        );
      },
    );
    print(res);
  }

  // TODO SimpleDialog
  void _showModalBottomSheet() {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        return SizedBox(
          height: 240,
          child: Column(
            children: [
              ListTile(
                title: Text('分享'),
                onTap: () {
                  Navigator.of(context).pop('分享');
                },
              ),
              Divider(),
              ListTile(
                title: Text('收藏'),
                onTap: () {
                  Navigator.of(context).pop('收藏');
                },
              ),
              Divider(),
              ListTile(
                title: Text('取消'),
                onTap: () {
                  Navigator.of(context).pop('取消');
                },
              ),
            ],
          ),
        );
      },
    );
  }

  //TODO 三方插件
  void _showToast() {
    Fluttertoast.showToast(
      msg: "This is Center Short Toast22222222",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0,
    );
  }

  // TODO 自定义dialog
  void _myDialog() {
    showDialog(
      context: context,
      builder: (context) {
        return MyDialog(
          title: '温馨提示',
          content: '这是内容',
          onTap: () {
            Navigator.of(context).pop('ok');
          },
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('你好flutter1')),
        body: Column(
          children: [
            ElevatedButton(
              onPressed: _alertDialog,
              child: Text('alertDialog'),
            ),
            ElevatedButton(
              onPressed: _simpleDialog,
              child: Text('simpleDialog'),
            ),
            ElevatedButton(
              onPressed: _showModalBottomSheet,
              child: Text('showModalBottomSheet'),
            ),
            ElevatedButton(
              onPressed: _showModalBottomSheet,
              child: Text('showModalBottomSheet'),
            ),
            ElevatedButton(
              onPressed: _showToast,
              child: Text('三方插件toast'),
            ),
            ElevatedButton(
              onPressed: _myDialog,
              child: Text('自定义dialog'),
            ),
          ],
        ),
      ),
    );
  }
}
